package com.imooc.bilibili.api;

import com.imooc.bilibili.domain.*;
//import com.imooc.bilibili.service.ElasticsearchService;
import com.imooc.bilibili.service.VideoService;
import com.imooc.bilibili.support.UserSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author xiexu
 * @create 2022-06-19 21:55
 */
@RestController
public class VideoApi {

    @Autowired
    private VideoService videoService;

    @Autowired
    private UserSupport userSupport;

//    @Autowired
//    private ElasticsearchService elasticsearchService;

    /**
     * 视频投稿(添加视频)
     *
     * @param video
     * @return
     */
    @PostMapping("/videos")
    public JsonResponse<String> addVideos(@RequestBody Video video) {
        Long userId = userSupport.getCurrentUserId();
        video.setUserId(userId);
        videoService.addVideos(video);
        // 在es中添加一条视频数据
        // 注意：一定要先在数据库添加之后才能在es中添加数据，因为在es中需要用到主键id，
        // 而主键id是需要在调用videoService.addVideos(video)后进行数据回写的
        //elasticsearchService.addVideo(video);
        return JsonResponse.success();
    }

    /**
     * 分页查询视频（前端瀑布流视频列表）
     *
     * @param size 每页显示多少条数据
     * @param no   要查询的是第几页
     * @param area 视频分区，加载一部分内容
     * @return
     */
    @GetMapping("/videos")
    public JsonResponse<PageResult<Video>> pageListVideos(Integer size, Integer no, String area) {
        PageResult<Video> result = videoService.pageListVideos(size, no, area);
        return new JsonResponse<>(result);
    }

    /**
     * 通过视频文件分片的形式，在线观看视频
     * <p>
     * 我们的数据是通过流的形式进行传输，所以这个流会写在http响应的输出流里面，
     * 所以我们不需要具体的返回类型
     *
     * @param request
     * @param response
     * @param url      文件的相对路径
     */
    @GetMapping("/video-slices")
    public void viewVideoOnlineBySlices(HttpServletRequest request, HttpServletResponse response, String url) throws Exception {
        videoService.viewVideoOnlineBySlices(request, response, url);
    }

    /**
     * 点赞视频
     * <p>
     * (@RequestParam Long videoId) 将参数单个单个传
     *
     * @param videoId 点赞视频的主键id
     * @return
     */
    @PostMapping("/video-likes")
    public JsonResponse<String> addVideoLike(@RequestParam Long videoId) {
        // 获取当前登录的用户id
        Long userId = userSupport.getCurrentUserId();
        videoService.addVideoLike(videoId, userId);
        return JsonResponse.success();
    }

    /**
     * 取消点赞视频
     *
     * @param videoId
     * @return
     */
    @DeleteMapping("/video-likes")
    public JsonResponse<String> deleteVideoLike(@RequestParam Long videoId) {
        Long userId = userSupport.getCurrentUserId();
        videoService.deleteVideoLike(videoId, userId);
        return JsonResponse.success();
    }

    /**
     * 查询视频点赞数量，如果用户已经登录，也是顺便查询当前用户有没有点赞过该视频(为了前端页面显示用的)
     * 注意：用户没有登录，可以观看视频，也可以查看视频的点赞数量
     *
     * @param videoId
     * @return
     */
    @GetMapping("/video-likes")
    public JsonResponse<Map<String, Object>> getVideoLikes(@RequestParam Long videoId) {
        Long userId = null;
        /**
         * 技巧：
         * 如果getCurrentUserId()发生异常，说明当前用户没有登录，那么userId直接用初始化的null值即可
         * 如果当前用户登录了，那么userId也会自动获取到当前用户的id
         * 这样子前端可以省去游客判断，后台也可以省去游客判断
         */
        try {
            userId = userSupport.getCurrentUserId();
        } catch (Exception ignored) {
        }
        // 获取当前视频被点赞的数量
        Map<String, Object> result = videoService.getVideoLikes(videoId, userId);
        return new JsonResponse<>(result);
    }

    /**
     * 收藏该视频
     * 注意：该写法可以和视频点赞的方法(addVideoLike)进行对比
     * (@RequestBody VideoCollection videoCollection) 将参数进行整体打包
     *
     * @param videoCollection
     * @return
     */
    @PostMapping("/video-collections")
    public JsonResponse<String> addVideoCollection(@RequestBody VideoCollection videoCollection) {
        Long userId = userSupport.getCurrentUserId();
        videoService.addVideoCollection(videoCollection, userId);
        return JsonResponse.success();
    }

    /**
     * 取消收藏视频
     *
     * @param videoId
     * @return
     */
    @DeleteMapping("/video-collections")
    public JsonResponse<String> deleteVideoCollection(@RequestParam Long videoId) {
        Long userId = userSupport.getCurrentUserId();
        videoService.deleteVideoCollection(videoId, userId);
        return JsonResponse.success();
    }

    /**
     * 查询该视频被收藏的数量，如果用户已经登录，顺便查询当前用户有没有收藏过该视频(为了前端页面显示用的)
     *
     * @param videoId
     * @return
     */
    @GetMapping("/video-collections")
    public JsonResponse<Map<String, Object>> getVideoCollections(@RequestParam Long videoId) {
        Long userId = null;
        try {
            userId = userSupport.getCurrentUserId();
        } catch (Exception ignored) {
        }
        Map<String, Object> result = videoService.getVideoCollections(videoId, userId);
        return new JsonResponse<>(result);
    }

    /**
     * 视频投币
     *
     * @param videoCoin
     * @return
     */
    @PostMapping("/video-coins")
    public JsonResponse<String> addVideoCoins(@RequestBody VideoCoin videoCoin) {
        Long userId = userSupport.getCurrentUserId();
        videoService.addVideoCoins(videoCoin, userId);
        return JsonResponse.success();
    }

    /**
     * 查询视频投币数量，如果用户已经登录，顺便查询当前用户有没有给该视频投过币(为了前端页面显示用的)
     *
     * @return
     */
    @GetMapping("/video-coins")
    public JsonResponse<Map<String, Object>> getVideoCoins(@RequestParam Long videoId) {
        Long userId = null;
        try {
            userId = userSupport.getCurrentUserId();
        } catch (Exception ignored) {
        }
        Map<String, Object> result = videoService.getVideoCoins(videoId, userId);
        return new JsonResponse<>(result);
    }

    /**
     * 添加视频评论
     *
     * @return
     */
    @PostMapping("/video-comments")
    public JsonResponse<String> addVideoComment(@RequestBody VideoComment videoComment) {
        Long userId = userSupport.getCurrentUserId();
        videoService.addVideoComment(videoComment, userId);
        return JsonResponse.success();
    }

    /**
     * 分页查询视频评论
     *
     * @param size    每页显示多少条数据
     * @param no      要查询的是第几页
     * @param videoId 视频id
     * @return
     * @RequestParam 表示该参数是必传的
     */
    @GetMapping("/video-comments")
    public JsonResponse<PageResult<VideoComment>> pageListVideoComments(@RequestParam Integer size, @RequestParam Integer no, @RequestParam Long videoId) {
        PageResult<VideoComment> result = videoService.pageListVideoComments(size, no, videoId);
        return new JsonResponse<>(result);
    }

    /**
     * 获取视频详情
     *
     * @param videoId
     * @return
     * @RequestParam 表示该参数是必传的
     */
    @GetMapping("/video-details")
    public JsonResponse<Map<String, Object>> getVideoDetails(@RequestParam Long videoId) {
        Map<String, Object> result = videoService.getVideoDetails(videoId);
        return new JsonResponse<>(result);
    }

    /**
     * 添加视频观看记录
     *
     * @param videoView
     * @param request
     * @return
     */
    @PostMapping("/video-views")
    public JsonResponse<String> addVideoView(@RequestBody VideoView videoView, HttpServletRequest request) {
        Long userId;
        try {
            // 判断是否是用户在观看视频，如果是用户就把用户id设置到videoView当中
            userId = userSupport.getCurrentUserId();
            videoView.setUserId(userId);
            videoService.addVideoView(videoView, request);
        } catch (Exception e) {
            // 如果是游客在观看视频，就直接调用添加方法
            videoService.addVideoView(videoView, request);
        }
        return JsonResponse.success();
    }

    /**
     * 查询视频播放量
     *
     * @param videoId
     * @return
     */
    @GetMapping("/video-view-counts")
    public JsonResponse<Integer> getVideoViewCounts(@RequestParam Long videoId) {
        Integer count = videoService.getVideoViewCounts(videoId);
        return new JsonResponse<>(count);
    }

}
